package view.menu;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import model.handlers.UserHandler;
import view.generaldesign.DesignLayout;
import view.ViewContainer;
import view.ViewHandler;
import view.ViewSingleton;

/**
 * 
 * @author Sonny Sandberg
 */
public class MenuLabelTop extends MenuLabel
{   
    public MenuLabelTop(String text, final view.Menu menu)
    {
        super(text, menu);
        
        setBounds(0, 0, 120, 30);
        setForeground(Color.WHITE);
        
        if (getMenuItem().getName().equals("LOG UD"))
        {
            setHorizontalAlignment(SwingConstants.RIGHT);
        }
        else
        {
            setHorizontalAlignment(SwingConstants.CENTER);
        }
        
        MouseAdapter mouseReleased;        
        mouseReleased =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseReleased(MouseEvent evt)
                        {
                            // Reset top menu background colors
                            view.ViewSingleton.getSingletonObject().getMenus()[0].resetBackgroundColor(DesignLayout.T_FIELD_BORDER_RED);
                            
                            // Reset left menu text colors
                            view.ViewSingleton.getSingletonObject().getMenus()[1].resetTextColor(Color.BLACK);
                            
                            if (getMenuItem().getName().equals("ADMIN"))
                            {
                                ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
                                for (ViewContainer value : allViews)
                                {
                                    value.getTheView().setVisible(false);
                                }
                                ViewHandler.toggleAllsView("designLayout", true);
                                ViewHandler.toggleAllsView("menuTop", true);
                                ViewHandler.toggleAllsView("menuLeft", true);
                                ViewHandler.toggleAllsView("userLoggedInPanel", true);
                                ViewHandler.toggleAllsView("adminCustomer", true);
                                ViewHandler.setPageTitle("Administrér Kunde");
                                ViewHandler.toggleAllsView("pageTitle", true);
                                ArrayList<MenuLabel> menuLabels = ViewSingleton.getSingletonObject().getMenuLabels();
                                for (MenuLabel value : menuLabels)
                                {
                                    if (value.getMenuLabelText().equals("ADMIN"))
                                    {
                                        value.setBackgroundColor();
                                    }
                                }
                                
                            }
                            else if (getMenuItem().getName().equals("LOG UD"))
                            {
                                boolean loggedOut = UserHandler.logout();
                                if (loggedOut == true)
                                {
                                    JPanel menuTop = ViewHandler.getAllViewsPanel("menuTop");
                                    menuTop.setVisible(false);
                                    menuTop.removeAll();
                                    menuTop.revalidate();
                                    menuTop.repaint();
                                    
                                    JPanel menuLeft = ViewHandler.getAllViewsPanel("menuLeft");
                                    menuLeft.setVisible(false);
                                    menuLeft.removeAll();
                                    menuLeft.revalidate();
                                    menuLeft.repaint();
                                    
                                    ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
                                    for (ViewContainer value : allViews)
                                    {
                                        value.getTheView().setVisible(false);
                                    }
                                    ViewHandler.toggleAllsView("designLayout", true);
                                    JPasswordField pFieldPassword = (JPasswordField) ViewHandler.getAllViewsPanel("login").getComponent(6);
                                    pFieldPassword.setText("");
                                    ViewHandler.toggleAllsView("login", true);
                                }
                            }
                            else
                            {
                                ArrayList<ViewContainer> allViews = view.ViewSingleton.getSingletonObject().getAllViews();
                                for (ViewContainer value : allViews)
                                {
                                    value.getTheView().setVisible(false);
                                }
                                ViewHandler.toggleAllsView("designLayout", true);
                                ViewHandler.toggleAllsView("menuTop", true);
                                ViewHandler.toggleAllsView("userLoggedInPanel", true);
                                ViewHandler.toggleAllsView(menu.getNameOfPanel(), true);
                            }                            
                        }
                    }
                );        
        
        this.addMouseListener(mouseReleased);
    }
}